package net.tngou.db;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;

public class SerializeUtils {

    public static byte[] fserialize(Serializable obj) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
        fserialize(obj, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    private static void fserialize(Serializable obj, ByteArrayOutputStream byteArrayOutputStream) {
        if (byteArrayOutputStream == null) {
            throw new IllegalArgumentException("The OutputStream must not be null");
        }
        FSTObjectOutput out = null;
        try {
            // stream closed in the finally
            out = new FSTObjectOutput(byteArrayOutputStream);
            out.writeObject(obj);
            out.flush();
        } catch (IOException ex) {
           
        } finally {
            try {
                if (out != null) {
                    out.close();
                    out=null;
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        }
    }
   
    
    public static Object fdeserialize(byte[] objectData) {
        if (objectData == null) {
            throw new IllegalArgumentException("The byte[] must not be null");
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(objectData);
        return fdeserialize(byteArrayInputStream);
    }
  
    private static Object fdeserialize(ByteArrayInputStream byteArrayInputStream) {
        if (byteArrayInputStream == null) {
            throw new IllegalArgumentException("The InputStream must not be null");
        }
        FSTObjectInput in = null;
        try {
            // stream closed in the finally
            in = new FSTObjectInput(byteArrayInputStream);
            return in.readObject();
        } catch (ClassNotFoundException ex) {
          
        } catch (IOException ex) {
           
        } finally {
            try {
                if (in != null) {
                    in.close();
                    in=null;
                }
            } catch (IOException ex) {
                // ignore close exception
            }
        }
		return null;
    }

   
    
    public static byte[] serialize(Object obj) {
        ObjectOutputStream oos = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(obj);
            return baos.toByteArray();
        } catch (IOException e) {
          
        } finally {
            if (oos != null)
                try {
                    oos.close();
                } catch (IOException e) {
                }
        }
        return null;
    }

    
    public static Object deserialize(byte[] bits) {
        ObjectInputStream ois = null;
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(bits);
            ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
            
        } finally {
            if (ois != null)
                try {
                    ois.close();
                } catch (IOException e) {
                }
        }
        return null;
    }
}
